using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Barrage.Global;
using SlimDX;

namespace Barrage.GUI
{
    /// <summary>
    /// A GUIElement represents an item inside a GUIScene. It can be interactive or not
    /// </summary>
    public abstract class GUIElement:Item
    {
        private const string XML_ID_ATTRIBUTE = "id";
        private const string XML_X_ATTRIBUTE = "x";
        private const string XML_Y_ATTRIBUTE = "y";

        private string template;
        /// <summary>
        /// The template that this item is using
        /// </summary>
        public string Template { get { return template; } }
        private string id;

        /// <summary>
        /// Unique string that identifies this item on the layout
        /// </summary>
        public string Id { get { return id; } set { id = value; } }

        private WeakReference layout;
        /// <summary>
        /// A reference to the layout that holds this item
        /// </summary>
        public GUILayout Layout { get { return (GUILayout)layout.Target; } }

        /// <summary>
        /// Constructor. Inits the element.
        /// </summary>
        /// <param name="layout">Layout that holds this item</param>
        /// <param name="template">Template that this item is using</param>
        /// <param name="id">Unique string that identifies this element</param>
        /// <param name="mesh">Mesh used to render the element</param>
        public GUIElement(GUILayout layout,string template,string id,Mesh mesh)
            : base(mesh)
        {
            this.layout = new WeakReference(layout);
            this.template = template;
            this.id = id;
        }

        /// <summary>
        /// Constructor. Inits the element.
        /// </summary>
        /// <param name="layout">Layout that holds this item</param>
        /// <param name="template">Template that this item is using</param>
        /// <param name="id">Unique string that identifies this element</param>
        public GUIElement(GUILayout layout, string template, string id)
            : base()
        {
            this.layout = new WeakReference(layout);
            this.template = template;
            this.id = id;
        }

        /// <summary>
        /// Constructor from a XML Node
        /// </summary>
        /// <param name="layout">Layout that holds this item</param>
        /// <param name="template">Template that this item is using</param>
        /// <param name="xn">Xml Node with parameters to this element</param>
        public GUIElement(GUILayout layout, string template, XmlNode xn)
            : base()
        {
            this.layout = new WeakReference(layout);
            this.template = template;
            this.id = XmlHelper.GetXMLAttribute(xn, XML_ID_ATTRIBUTE);
            this.motion.Translation = new Vector3(GUIHelper.ParseVector2(XmlHelper.GetXMLAttribute(xn, XML_X_ATTRIBUTE), XmlHelper.GetXMLAttribute(xn,XML_Y_ATTRIBUTE), Vector2.Zero), 0);
        }
    }
}
